package com.ruoyi.web.controller.system;

import java.util.List;

import com.ruoyi.common.annotation.Anonymous;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.Houses;
import com.ruoyi.system.service.IHousesService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 出租的房屋Controller
 * 
 * @author ruoyi
 * @date 2024-12-30
 */
@RestController
@RequestMapping("/system/houses")
public class HousesController extends BaseController
{
    @Autowired
    private IHousesService housesService;

    /**
     * 查询出租的房屋列表
     */
    @PreAuthorize("@ss.hasPermi('system:houses:list')")
    @GetMapping("/list")
    public TableDataInfo list(Houses houses)
    {
        startPage();
        List<Houses> list = housesService.selectHousesList(houses);
        return getDataTable(list);
    }

    /**
     * 微信查询出租的房屋列表
     */
    @Anonymous
    @GetMapping("/wxlist")
    public TableDataInfo wxlist(Houses houses)
    {
        startPage();
        List<Houses> list = housesService.wxHousesList(houses);
        return getDataTable(list);
    }

    /**
     * 根据经续度查询出租的房屋列表
     * 以1公里为距离范围内
     * @param lat 续度
     * @param lng 经度
     * @return 出租的房屋集合
     */
    @Anonymous
    @GetMapping(value = "/selectHousesByLatLng/{lat}/{lng}/{distance}")
    public AjaxResult selectHousesByLatLng(@PathVariable("lat") Double lat,@PathVariable("lng") Double lng,@PathVariable("distance") Double distance)
    {
        return success(housesService.selectHousesByLatLng(lat,lng,distance));
    }

    /**
     * 导出出租的房屋列表
     */
    @PreAuthorize("@ss.hasPermi('system:houses:export')")
    @Log(title = "出租的房屋", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Houses houses)
    {
        List<Houses> list = housesService.selectHousesList(houses);
        ExcelUtil<Houses> util = new ExcelUtil<Houses>(Houses.class);
        util.exportExcel(response, list, "出租的房屋数据");
    }

    /**
     * 获取出租的房屋详细信息
     */
//    @PreAuthorize("@ss.hasPermi('system:houses:query')")
    @Anonymous
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(housesService.selectHousesById(id));
    }

    /**
     * 新增出租的房屋
     */
    @PreAuthorize("@ss.hasPermi('system:houses:add')")
    @Log(title = "出租的房屋", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Houses houses)
    {
        houses.setCreateBy(getUsername());
        return toAjax(housesService.insertHouses(houses));
    }

    /**
     * 修改出租的房屋
     */
    @PreAuthorize("@ss.hasPermi('system:houses:edit')")
    @Log(title = "出租的房屋", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Houses houses)
    {
        houses.setUpdateBy(getUsername());
        return toAjax(housesService.updateHouses(houses));
    }

    /**
     * 删除出租的房屋
     */
    @PreAuthorize("@ss.hasPermi('system:houses:remove')")
    @Log(title = "出租的房屋", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(housesService.deleteHousesByIds(ids));
    }
}
